<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Denzel - 让你的网页开口说话 —— audioContext API</title>
    <meta name="description" content="前端笔记">
    <meta name="keywords" content="web前端,HTML5,CSS3,Javascript,SVG,canvas">
  <meta name="anthor" content="Denzel">
  <meta name="theme-color" content="#3eaf7c">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="msapplication-TileImage" content="/icons/msapplication-icon-144x144.png">
  <meta name="msapplication-TileColor" content="#000000">
  <link rel="icon" href="/blog/vuepress/logo.png">
  <link rel="manifest" href="/blog/vuepress/manifest.json">
  <link rel="apple-touch-icon" href="/blog/vuepress/icons/apple-touch-icon-152x152.png">
  <link rel="mask-icon" href="/blog/vuepress/icons/safari-pinned-tab.svg" color="#3eaf7c">
    
    <link rel="preload" href="/blog/vuepress/assets/css/0.styles.d4f29607.css" as="style"><link rel="preload" href="/blog/vuepress/assets/js/app.15770e34.js" as="script"><link rel="preload" href="/blog/vuepress/assets/js/28.e320efab.js" as="script"><link rel="preload" href="/blog/vuepress/assets/js/94.e993d310.js" as="script"><link rel="preload" href="/blog/vuepress/assets/js/93.c6ca00b3.js" as="script"><link rel="preload" href="/blog/vuepress/assets/js/92.80ab06fd.js" as="script"><link rel="preload" href="/blog/vuepress/assets/js/91.b493a1bb.js" as="script"><link rel="preload" href="/blog/vuepress/assets/js/90.c1e3263e.js" as="script"><link rel="preload" href="/blog/vuepress/assets/js/89.b05297be.js" as="script"><link rel="prefetch" href="/blog/vuepress/assets/js/46.59c0ece7.js"><link rel="prefetch" href="/blog/vuepress/assets/js/1.05e4270f.js"><link rel="prefetch" href="/blog/vuepress/assets/js/2.ace739f7.js"><link rel="prefetch" href="/blog/vuepress/assets/js/3.636e5a79.js"><link rel="prefetch" href="/blog/vuepress/assets/js/4.7a55aa4b.js"><link rel="prefetch" href="/blog/vuepress/assets/js/5.b56b0573.js"><link rel="prefetch" href="/blog/vuepress/assets/js/6.e9a3fbc8.js"><link rel="prefetch" href="/blog/vuepress/assets/js/7.aa46c47d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/8.a9288ac6.js"><link rel="prefetch" href="/blog/vuepress/assets/js/9.b9f8dec6.js"><link rel="prefetch" href="/blog/vuepress/assets/js/10.3e2ea470.js"><link rel="prefetch" href="/blog/vuepress/assets/js/11.4d115f98.js"><link rel="prefetch" href="/blog/vuepress/assets/js/12.c97cf4d0.js"><link rel="prefetch" href="/blog/vuepress/assets/js/13.1e0cf594.js"><link rel="prefetch" href="/blog/vuepress/assets/js/14.bab9026a.js"><link rel="prefetch" href="/blog/vuepress/assets/js/15.1a24cffe.js"><link rel="prefetch" href="/blog/vuepress/assets/js/16.e9995850.js"><link rel="prefetch" href="/blog/vuepress/assets/js/17.86a1f7d2.js"><link rel="prefetch" href="/blog/vuepress/assets/js/18.c32400c4.js"><link rel="prefetch" href="/blog/vuepress/assets/js/19.9f99887c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/20.82f0c2f9.js"><link rel="prefetch" href="/blog/vuepress/assets/js/21.7f5c223d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/22.acf9934c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/23.20d8e817.js"><link rel="prefetch" href="/blog/vuepress/assets/js/24.acf27379.js"><link rel="prefetch" href="/blog/vuepress/assets/js/25.20165510.js"><link rel="prefetch" href="/blog/vuepress/assets/js/26.83a2ddc3.js"><link rel="prefetch" href="/blog/vuepress/assets/js/27.33276a04.js"><link rel="prefetch" href="/blog/vuepress/assets/js/29.3fd92c30.js"><link rel="prefetch" href="/blog/vuepress/assets/js/30.2bc84d43.js"><link rel="prefetch" href="/blog/vuepress/assets/js/31.d70ed500.js"><link rel="prefetch" href="/blog/vuepress/assets/js/32.e7a0f8e1.js"><link rel="prefetch" href="/blog/vuepress/assets/js/33.c48ee70b.js"><link rel="prefetch" href="/blog/vuepress/assets/js/34.8ad03d1b.js"><link rel="prefetch" href="/blog/vuepress/assets/js/35.5bdb3994.js"><link rel="prefetch" href="/blog/vuepress/assets/js/36.a25f926e.js"><link rel="prefetch" href="/blog/vuepress/assets/js/37.7515cfbb.js"><link rel="prefetch" href="/blog/vuepress/assets/js/38.99347371.js"><link rel="prefetch" href="/blog/vuepress/assets/js/39.3481fc62.js"><link rel="prefetch" href="/blog/vuepress/assets/js/40.bc709734.js"><link rel="prefetch" href="/blog/vuepress/assets/js/41.e1e0a3bf.js"><link rel="prefetch" href="/blog/vuepress/assets/js/42.762b61b0.js"><link rel="prefetch" href="/blog/vuepress/assets/js/43.8f0b2d6c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/44.2a0a4ee0.js"><link rel="prefetch" href="/blog/vuepress/assets/js/45.baf7ed60.js"><link rel="prefetch" href="/blog/vuepress/assets/js/47.11a8720a.js"><link rel="prefetch" href="/blog/vuepress/assets/js/48.e0bf8b54.js"><link rel="prefetch" href="/blog/vuepress/assets/js/49.c96c4011.js"><link rel="prefetch" href="/blog/vuepress/assets/js/50.31189263.js"><link rel="prefetch" href="/blog/vuepress/assets/js/51.766a8294.js"><link rel="prefetch" href="/blog/vuepress/assets/js/52.26b971fd.js"><link rel="prefetch" href="/blog/vuepress/assets/js/53.0cfbd708.js"><link rel="prefetch" href="/blog/vuepress/assets/js/54.bdc15c87.js"><link rel="prefetch" href="/blog/vuepress/assets/js/55.fea285a9.js"><link rel="prefetch" href="/blog/vuepress/assets/js/56.e030a6b2.js"><link rel="prefetch" href="/blog/vuepress/assets/js/57.bd168a1e.js"><link rel="prefetch" href="/blog/vuepress/assets/js/58.636557fa.js"><link rel="prefetch" href="/blog/vuepress/assets/js/59.5d1d0ce6.js"><link rel="prefetch" href="/blog/vuepress/assets/js/60.0b779f92.js"><link rel="prefetch" href="/blog/vuepress/assets/js/61.4ec3f2eb.js"><link rel="prefetch" href="/blog/vuepress/assets/js/62.8f70f114.js"><link rel="prefetch" href="/blog/vuepress/assets/js/63.9ac2b61d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/64.c028fad5.js"><link rel="prefetch" href="/blog/vuepress/assets/js/65.923a8d15.js"><link rel="prefetch" href="/blog/vuepress/assets/js/66.73db652e.js"><link rel="prefetch" href="/blog/vuepress/assets/js/67.1a91446d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/68.e5a12a42.js"><link rel="prefetch" href="/blog/vuepress/assets/js/69.6ddcb882.js"><link rel="prefetch" href="/blog/vuepress/assets/js/70.ceb70d85.js"><link rel="prefetch" href="/blog/vuepress/assets/js/71.a804ef40.js"><link rel="prefetch" href="/blog/vuepress/assets/js/72.f1c4098c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/73.2f28b842.js"><link rel="prefetch" href="/blog/vuepress/assets/js/74.571c2ab1.js"><link rel="prefetch" href="/blog/vuepress/assets/js/75.f365c2a1.js"><link rel="prefetch" href="/blog/vuepress/assets/js/76.0916a3cd.js"><link rel="prefetch" href="/blog/vuepress/assets/js/77.469aed17.js"><link rel="prefetch" href="/blog/vuepress/assets/js/78.02cdd59d.js"><link rel="prefetch" href="/blog/vuepress/assets/js/79.44d7f95c.js"><link rel="prefetch" href="/blog/vuepress/assets/js/80.4ca072f1.js"><link rel="prefetch" href="/blog/vuepress/assets/js/81.d9239a49.js"><link rel="prefetch" href="/blog/vuepress/assets/js/82.b09096b2.js"><link rel="prefetch" href="/blog/vuepress/assets/js/83.f4bc8a9b.js"><link rel="prefetch" href="/blog/vuepress/assets/js/84.1ffe6969.js"><link rel="prefetch" href="/blog/vuepress/assets/js/85.44e980f9.js"><link rel="prefetch" href="/blog/vuepress/assets/js/86.c8579ac0.js"><link rel="prefetch" href="/blog/vuepress/assets/js/87.cd49f324.js"><link rel="prefetch" href="/blog/vuepress/assets/js/88.3851b6ff.js"><link rel="prefetch" href="/blog/vuepress/assets/js/95.8e19a7ed.js">
    <link rel="stylesheet" href="/blog/vuepress/assets/css/0.styles.d4f29607.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div><a href="/blog/vuepress/" class="home-link router-link-active"><img src="/blog/vuepress/hero.png" class="logo"><span class="site-name can-hide">
      Denzel
    </span></a><div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""><!----></div><nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/vuepress/js/" class="nav-link router-link-active">JS</a></div><div class="nav-item"><a href="/blog/vuepress/css/" class="nav-link">CSS</a></div><div class="nav-item"><a href="/blog/vuepress/html/" class="nav-link">HTML</a></div><div class="nav-item"><a href="/blog/vuepress/else/" class="nav-link">其他</a></div><div class="nav-item"><a href="/blog/vuepress/study/" class="nav-link">study</a></div><a href="https://github.com/xiaotianxia" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header><div class="sidebar-mask"></div><div class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/blog/vuepress/js/" class="nav-link router-link-active">JS</a></div><div class="nav-item"><a href="/blog/vuepress/css/" class="nav-link">CSS</a></div><div class="nav-item"><a href="/blog/vuepress/html/" class="nav-link">HTML</a></div><div class="nav-item"><a href="/blog/vuepress/else/" class="nav-link">其他</a></div><div class="nav-item"><a href="/blog/vuepress/study/" class="nav-link">study</a></div><a href="https://github.com/xiaotianxia" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav><ul class="sidebar-links"><li><div class="sidebar-group first"><p class="sidebar-heading open"><span>JS</span><!----></p><ul class="sidebar-group-items"><li><a href="/blog/vuepress/js/" class="sidebar-link">目录</a></li><li><a href="/blog/vuepress/js/four_kinds_of_observers.html" class="sidebar-link">JS中的观察者们 —— 四种 Observers</a></li><li><a href="/blog/vuepress/js/editable_dom.html" class="sidebar-link">可编辑的页面——designMode、contenteditable和user-modify</a></li><li><a href="/blog/vuepress/js/useful_webapis_mediarecorder.html" class="sidebar-link">用浏览器轻松录制音频、视频—— MediaRecorder API</a></li><li><a href="/blog/vuepress/js/useful_webapis_audiocontext.html" class="active sidebar-link">让你的网页开口说话 —— audioContext API</a></li><li><a href="/blog/vuepress/js/useful_webapis_fullscreen.html" class="sidebar-link">几个有用的Web API——全屏模式</a></li><li><a href="/blog/vuepress/js/useful_webapis_notification.html" class="sidebar-link">几个有用的Web API——桌面通知API</a></li><li><a href="/blog/vuepress/js/useful_webapis_connection.html" class="sidebar-link">几个有用的Web API——网络状态API</a></li><li><a href="/blog/vuepress/js/useful_webapis_geolocation.html" class="sidebar-link">几个有用的Web API——地理位置API</a></li><li><a href="/blog/vuepress/js/speech_in_js_recognition.html" class="sidebar-link">JS中的语音识别——Speech Recognition API</a></li><li><a href="/blog/vuepress/js/speech_in_js_synthesis.html" class="sidebar-link">JS中的语音合成——Speech Synthesis API</a></li><li><a href="/blog/vuepress/js/camera_in_js_trial.html" class="sidebar-link">JS控制设备摄像头初探</a></li><li><a href="/blog/vuepress/js/shape_detection_api_trial.html" class="sidebar-link">浏览器的图像识别API初探</a></li><li><a href="/blog/vuepress/js/simple_chatroom_using_socketio.html" class="sidebar-link">socket.io实现简单群聊</a></li><li><a href="/blog/vuepress/js/communication_using_postmessage.html" class="sidebar-link">postMessage实现父子页面简单通信</a></li><li><a href="/blog/vuepress/js/upload_using_filereader_createobjecturl.html" class="sidebar-link">FileReader与URL.createObjectURL实现图片、视频上传预览</a></li><li><a href="/blog/vuepress/js/simple_code_preview.html" class="sidebar-link">前端几行代码简单实现w3school代码预览</a></li></ul></div></li></ul></div><div class="page"><div class="content"><h1 id="让你的网页开口说话-——-audiocontext-api"><a href="#让你的网页开口说话-——-audiocontext-api" aria-hidden="true" class="header-anchor">#</a> 让你的网页开口说话 —— audioContext API</h1><p><a href="https://denzel.netlify.com/js/useful_webapis_audiocontext.html?_=65312387656512" target="_blank" rel="noopener noreferrer">原文链接</a></p><blockquote><p>浏览器可以利用CSS3、SVG和canvas处理图像和作画，简直成了简易版的Photoshop。
最近研究了一下audioContext，才发现水深不输他们啊！虽然自己出身通信专业，也学过语音信号处理，
但对里面的基本原理还是知之甚少，深感对不起母校的培养...</p></blockquote><h2 id="前言"><a href="#前言" aria-hidden="true" class="header-anchor">#</a> 前言</h2><p>AudioContext设计的API简直太多了，而且目前好多API还处在实验阶段，并未被所有浏览器完全支持，
所以本文不打算覆盖所有知识，只简单学习几个常用的API。先知其然，再知其所以然，也不失为一种学习方法嘛。
(其实好多我是真的不懂 😂)</p><h2 id="简介"><a href="#简介" aria-hidden="true" class="header-anchor">#</a> 简介</h2><p>先来一个简单的栗子🌰↓，感受下它能做什么——</p><h3 id="demo1"><a href="#demo1" aria-hidden="true" class="header-anchor">#</a> demo1</h3><div class="audio-wrapper" data-v-f2c0b5e4><form class="el-form el-form--label-left" data-v-f2c0b5e4><div class="el-form-item" data-v-f2c0b5e4><label class="el-form-item__label" style="width:120px;">音量:</label><div class="el-form-item__content" style="margin-left:120px;"><div role="slider" aria-valuemin="0" aria-valuemax="1" aria-orientation="horizontal" class="el-slider el-slider--with-input" data-v-f2c0b5e4><div debounce="300" class="el-slider__input el-input-number el-input-number--small"><span role="button" class="el-input-number__decrease"><i class="el-icon-minus"></i></span><span role="button" class="el-input-number__increase"><i class="el-icon-plus"></i></span><div class="el-input el-input--small"><!----><input type="text" autocomplete="off" max="1" min="0" value="0" class="el-input__inner"><!----><!----><!----></div></div><div class="el-slider__runway show-input"><div class="el-slider__bar" style="width:0%;left:0%;"></div><div tabindex="0" class="el-slider__button-wrapper" style="left:0%;"><div class="el-slider__button el-tooltip"></div></div><!----><div class="el-slider__stop" style="left:10%;"></div><div class="el-slider__stop" style="left:20%;"></div><div class="el-slider__stop" style="left:30%;"></div><div class="el-slider__stop" style="left:40%;"></div><div class="el-slider__stop" style="left:50%;"></div><div class="el-slider__stop" style="left:60%;"></div><div class="el-slider__stop" style="left:70%;"></div><div class="el-slider__stop" style="left:80%;"></div><div class="el-slider__stop" style="left:90%;"></div></div></div><!----></div></div><div class="el-form-item" data-v-f2c0b5e4><label class="el-form-item__label" style="width:120px;">音量变化曲线:</label><div class="el-form-item__content" style="margin-left:120px;"><div class="el-select el-select--small" data-v-f2c0b5e4><!----><div class="el-input el-input--small el-input--suffix"><!----><input type="text" autocomplete="off" placeholder="请选择" readonly="readonly" value="" class="el-input__inner"><!----><span class="el-input__suffix"><span class="el-input__suffix-inner"><i class="el-select__caret el-input__icon el-icon-arrow-up"></i><!----></span><!----></span><!----></div><div class="el-select-dropdown el-popper" style="min-width:;display:none;"><div class="el-scrollbar" style="display:none;"><div class="el-select-dropdown__wrap el-scrollbar__wrap el-scrollbar__wrap--hidden-default"><ul class="el-scrollbar__view el-select-dropdown__list"><!----><li class="el-select-dropdown__item selected" data-v-f2c0b5e4><span>线性</span></li><li class="el-select-dropdown__item" data-v-f2c0b5e4><span>指数</span></li></ul></div><div class="el-scrollbar__bar is-horizontal"><div class="el-scrollbar__thumb" style="width:0;transform:translateX(0%);ms-transform:translateX(0%);webkit-transform:translateX(0%);"></div></div><div class="el-scrollbar__bar is-vertical"><div class="el-scrollbar__thumb" style="height:0;transform:translateY(0%);ms-transform:translateY(0%);webkit-transform:translateY(0%);"></div></div></div><p class="el-select-dropdown__empty">
        无数据
      </p></div></div><!----></div></div><div class="el-form-item" data-v-f2c0b5e4><label class="el-form-item__label" style="width:120px;">波形:</label><div class="el-form-item__content" style="margin-left:120px;"><div class="el-select el-select--small" data-v-f2c0b5e4><!----><div class="el-input el-input--small el-input--suffix"><!----><input type="text" autocomplete="off" placeholder="请选择" readonly="readonly" value="" class="el-input__inner"><!----><span class="el-input__suffix"><span class="el-input__suffix-inner"><i class="el-select__caret el-input__icon el-icon-arrow-up"></i><!----></span><!----></span><!----></div><div class="el-select-dropdown el-popper" style="min-width:;display:none;"><div class="el-scrollbar" style="display:none;"><div class="el-select-dropdown__wrap el-scrollbar__wrap el-scrollbar__wrap--hidden-default"><ul class="el-scrollbar__view el-select-dropdown__list"><!----><li class="el-select-dropdown__item selected" data-v-f2c0b5e4><span>sine</span></li><li class="el-select-dropdown__item" data-v-f2c0b5e4><span>square</span></li><li class="el-select-dropdown__item" data-v-f2c0b5e4><span>sawtooth</span></li><li class="el-select-dropdown__item" data-v-f2c0b5e4><span>triangle</span></li><li class="el-select-dropdown__item is-disabled" data-v-f2c0b5e4><span>custom</span></li></ul></div><div class="el-scrollbar__bar is-horizontal"><div class="el-scrollbar__thumb" style="width:0;transform:translateX(0%);ms-transform:translateX(0%);webkit-transform:translateX(0%);"></div></div><div class="el-scrollbar__bar is-vertical"><div class="el-scrollbar__thumb" style="height:0;transform:translateY(0%);ms-transform:translateY(0%);webkit-transform:translateY(0%);"></div></div></div><p class="el-select-dropdown__empty">
        无数据
      </p></div></div><!----></div></div><div class="el-form-item" data-v-f2c0b5e4><label class="el-form-item__label" style="width:120px;">频率(Hz):</label><div class="el-form-item__content" style="margin-left:120px;"><div role="slider" aria-valuemin="1" aria-valuemax="2000" aria-orientation="horizontal" class="el-slider el-slider--with-input" data-v-f2c0b5e4><div debounce="300" class="el-slider__input el-input-number el-input-number--small"><span role="button" class="el-input-number__decrease"><i class="el-icon-minus"></i></span><span role="button" class="el-input-number__increase"><i class="el-icon-plus"></i></span><div class="el-input el-input--small"><!----><input type="text" autocomplete="off" max="2000" min="1" value="1" class="el-input__inner"><!----><!----><!----></div></div><div class="el-slider__runway show-input"><div class="el-slider__bar" style="width:-0.05002501250625312%;left:0%;"></div><div tabindex="0" class="el-slider__button-wrapper" style="left:-0.05002501250625312%;"><div class="el-slider__button el-tooltip"></div></div><!----></div></div><!----></div></div><div class="el-form-item" data-v-f2c0b5e4><!----><div class="el-form-item__content" style="margin-left:120px;"><button type="button" class="el-button el-button--primary el-button--small" data-v-f2c0b5e4><!----><!----><span>开始</span></button><!----></div></div></form></div><p>注意这可不是用audio标签播放的音频的文件，而是用js控制浏览器发出的声音哈。可以调节音量、频率等参数再试听一下。</p><p>怎么实现的呢？先看下面这个图 ——
<img src="http://pn4meizzc.bkt.clouddn.com/audio-context.png" alt>
这就是实现出声必须的流程图。首先需要一个音频环境(audio context)， 然后有一个音频数据的输入(input)，再加上处理程序(effects)，然后是音频的输出(destination)，最后把他们连起来(connection) ——</p><p>音频数据的输入可以是自定义的数据、页面上的audio元素的音频、从用户设备输入的音频或视频、远程的音频文件等。</p><p>处理程序可以是分析器、处理器等，也可以没有，即直接将声源连接到扬声器。</p><p>音频的输出一般就是用户设备的扬声器。</p><p>创建一个AudioContext实例，这是音频处理程序运行的环境</p><pre class="language-js"><code><span class="token keyword">let</span> audioCtx <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">AudioContext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
</code></pre><p>创建一个振荡器，这是声音的源头</p><pre class="language-js"><code><span class="token keyword">let</span> oscillator <span class="token operator">=</span> audioCtx<span class="token punctuation">.</span><span class="token function">createOscillator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><p>创建一个增益节点(音量节点)，用来调节音量的变化</p><pre class="language-js"><code><span class="token keyword">let</span> gainNode <span class="token operator">=</span> audioCtx<span class="token punctuation">.</span><span class="token function">createGain</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><p>设置音量和振荡器参数</p><pre class="language-js"><code>gainNode<span class="token punctuation">.</span>gain<span class="token punctuation">.</span>value <span class="token operator">=</span> <span class="token number">0.5</span><span class="token punctuation">;</span>  <span class="token comment">// 音量 0~1</span>
oscillator<span class="token punctuation">.</span>type <span class="token operator">=</span> <span class="token string">'sine'</span><span class="token punctuation">;</span>   <span class="token comment">// 振荡器输出正弦波</span>
oscillator<span class="token punctuation">.</span>frequency<span class="token punctuation">.</span>value <span class="token operator">=</span> <span class="token number">200</span><span class="token punctuation">;</span>  <span class="token comment">// 振荡频率200Hz</span>
</code></pre><p>各种连接，参照上面的流程图</p><pre class="language-js"><code>oscillator<span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span>gainNode<span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">// 发生源振荡器连接音量</span>
gainNode<span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span>audioCtx<span class="token punctuation">.</span>destination<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//音量连接扬声器</span>
</code></pre><p>开始发声</p><pre class="language-js"><code>oscillator<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><p>结束发声</p><pre class="language-js"><code>oscillator<span class="token punctuation">.</span><span class="token function">stop</span><span class="token punctuation">(</span>audioCtx<span class="token punctuation">.</span>currentTime <span class="token operator">+</span> <span class="token constant">FADING_TIME</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//现在起FADING_TIME秒后结束发声，没有FADING_TIME表示立刻结束</span>
</code></pre><p>以下的demo中，整体大致都是这个流程：创建声源→中间处理程序→扬声器。当然如果不需要输出音效，就不需要连接到audioCtx.destination即可。</p><p>除此之外，还可以设置声音的变化曲线，API提供两个方法：linearRampToValueAtTime 和 exponentialRampToValueAtTime ，分别表示线性变化和指数变化。</p><p>完整代码：</p><pre class="language-js"><code><span class="token keyword">const</span> <span class="token constant">FADING_TIME</span> <span class="token operator">=</span> <span class="token number">0.5</span><span class="token punctuation">;</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
    <span class="token function">data</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token punctuation">{</span>
            started<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
            queryParams<span class="token punctuation">:</span> <span class="token punctuation">{</span>
                gain<span class="token punctuation">:</span> <span class="token number">0.5</span><span class="token punctuation">,</span>
                gainChangeType<span class="token punctuation">:</span> <span class="token string">'linearRampToValueAtTime'</span><span class="token punctuation">,</span>
                frequency<span class="token punctuation">:</span> <span class="token number">196</span><span class="token punctuation">,</span>
                waveform<span class="token punctuation">:</span> <span class="token string">'sine'</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>

    methods<span class="token punctuation">:</span> <span class="token punctuation">{</span>
        <span class="token function">init</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>oscillator <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>audioCtx<span class="token punctuation">.</span><span class="token function">createOscillator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>gainNode <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>audioCtx<span class="token punctuation">.</span><span class="token function">createGain</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>

        <span class="token function">onSet</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>oscillator<span class="token punctuation">.</span>type <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>queryParams<span class="token punctuation">.</span>waveform<span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>gainNode<span class="token punctuation">.</span>gain<span class="token punctuation">.</span>value <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>queryParams<span class="token punctuation">.</span>gain<span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>oscillator<span class="token punctuation">.</span>frequency<span class="token punctuation">.</span>value <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>queryParams<span class="token punctuation">.</span>frequency<span class="token punctuation">;</span>
            
            <span class="token keyword">this</span><span class="token punctuation">.</span>oscillator<span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>gainNode<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>gainNode<span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>audioCtx<span class="token punctuation">.</span>destination<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>

        <span class="token function">onStart</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">onSet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>oscillator<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token keyword">this</span><span class="token punctuation">.</span>started <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>

        <span class="token function">onStop</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        	<span class="token comment">//先在0.5秒内变化到0.001，然后停止</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>gainNode<span class="token punctuation">.</span>gain<span class="token punctuation">[</span><span class="token keyword">this</span><span class="token punctuation">.</span>queryParams<span class="token punctuation">.</span>gainChangeType<span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token number">0.001</span><span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>audioCtx<span class="token punctuation">.</span>currentTime <span class="token operator">+</span> <span class="token constant">FADING_TIME</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>oscillator<span class="token punctuation">.</span><span class="token function">stop</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>audioCtx<span class="token punctuation">.</span>currentTime <span class="token operator">+</span> <span class="token constant">FADING_TIME</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token keyword">this</span><span class="token punctuation">.</span>started <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>

    <span class="token function">mounted</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>AudioContext <span class="token operator">&amp;&amp;</span> <span class="token operator">!</span>webkitAudioContext<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">'您的浏览器不支持audioContext!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>audioCtx <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token punctuation">(</span>AudioContext <span class="token operator">||</span> webkitAudioContext<span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre><h3 id="demo2"><a href="#demo2" aria-hidden="true" class="header-anchor">#</a> demo2</h3><p>既然可以控制声音的频率，那么“哆来咪”这样的简单音符也是可以的了。查了一下<a href="http://www.360doc.com/content/11/0815/08/3416571_140455810.shtml" target="_blank" rel="noopener noreferrer">钢琴键的频率表</a>，
选取了一段频率，便可以做成一个简单的小钢琴了。看下面的栗子🌰↓</p><div class="audio-wrapper" data-v-c8e24eba><ul class="piano" data-v-c8e24eba><li class="key white" data-v-c8e24eba></li><li class="key black" data-v-c8e24eba></li><li class="key white" data-v-c8e24eba></li><li class="key black" data-v-c8e24eba></li><li class="key white" data-v-c8e24eba></li><li class="key white" data-v-c8e24eba></li><li class="key black" data-v-c8e24eba></li><li class="key white" data-v-c8e24eba></li><li class="key black" data-v-c8e24eba></li><li class="key white" data-v-c8e24eba></li><li class="key black" data-v-c8e24eba></li><li class="key white" data-v-c8e24eba></li></ul></div><p>代码同demo1相比，只不过把频率设置成固定的几个而已。</p><pre class="language-js"><code><span class="token keyword">const</span> <span class="token constant">FADING_TIME</span> <span class="token operator">=</span> <span class="token number">0.5</span><span class="token punctuation">;</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
    <span class="token function">data</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token punctuation">{</span>
            <span class="token constant">FREQUENCY_LIST</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token number">261.626</span><span class="token punctuation">,</span> <span class="token number">277.183</span><span class="token punctuation">,</span> <span class="token number">293.665</span><span class="token punctuation">,</span> <span class="token number">311.127</span><span class="token punctuation">,</span> <span class="token number">329.628</span><span class="token punctuation">,</span> <span class="token number">349.228</span><span class="token punctuation">,</span> <span class="token number">369.994</span><span class="token punctuation">,</span> <span class="token number">391.995</span><span class="token punctuation">,</span> <span class="token number">415.305</span><span class="token punctuation">,</span> <span class="token number">440.000</span><span class="token punctuation">,</span> <span class="token number">466.164</span><span class="token punctuation">,</span> <span class="token number">493.883</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
            queryParams<span class="token punctuation">:</span> <span class="token punctuation">{</span>
                gain<span class="token punctuation">:</span> <span class="token number">0.5</span><span class="token punctuation">,</span>
                frequency<span class="token punctuation">:</span> <span class="token number">196</span><span class="token punctuation">,</span>
                waveform<span class="token punctuation">:</span> <span class="token string">'sine'</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>

    methods<span class="token punctuation">:</span> <span class="token punctuation">{</span>
        <span class="token function">init</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>oscillator <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>audioCtx<span class="token punctuation">.</span><span class="token function">createOscillator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>gainNode <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>audioCtx<span class="token punctuation">.</span><span class="token function">createGain</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
       
            <span class="token keyword">this</span><span class="token punctuation">.</span>oscillator<span class="token punctuation">.</span>type <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>queryParams<span class="token punctuation">.</span>waveform<span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>gainNode<span class="token punctuation">.</span>gain<span class="token punctuation">.</span>value <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>queryParams<span class="token punctuation">.</span>gain<span class="token punctuation">;</span>

            <span class="token keyword">this</span><span class="token punctuation">.</span>oscillator<span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>gainNode<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>gainNode<span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>audioCtx<span class="token punctuation">.</span>destination<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>

        <span class="token function">onPlay</span> <span class="token punctuation">(</span>item<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>oscillator<span class="token punctuation">.</span>frequency<span class="token punctuation">.</span>value <span class="token operator">=</span> item<span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>oscillator<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>audioCtx<span class="token punctuation">.</span>currentTime<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>oscillator<span class="token punctuation">.</span><span class="token function">stop</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>audioCtx<span class="token punctuation">.</span>currentTime <span class="token operator">+</span> <span class="token constant">FADING_TIME</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>

    <span class="token function">mounted</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>AudioContext <span class="token operator">&amp;&amp;</span> <span class="token operator">!</span>webkitAudioContext<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">'您的浏览器不支持audioContext!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>audioCtx <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token punctuation">(</span>AudioContext <span class="token operator">||</span> webkitAudioContext<span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre><p>上文说了，音频数据的输入可以是自定义的数据、页面上的audio元素的音频、从用户设备输入的音频或视频、远程的音频文件等。
下面给出几个demo来演示声音来源的几种不同情况，并且用canvas展示了音频的频率变化图。</p><div class="warning custom-block"><p class="custom-block-title">注意</p><p>有的浏览器会出现报错：<span class="txt-red">&quot;NotSupportedError: Failed to construct 'AudioContext': The number of hardware contexts provided (6) is greater than or equal to the maximum bound (6).&quot;</span>，
原因猜测是有的版本限制了网页同时存在的audioContext实例的数量（限制为6个？），因为每个demo里至少会传建一个实例，可能会超过限制导致报错。</p><p>我用的chromium(58+)会报错，chrome(68+)就不会，具体支持情况没做深究。同学们如果遇到报错情况，不妨试下升级浏览器。</p></div><p>除了上面的方法外，还有两个比较重要的方法：<strong>createAnalyser</strong>、<strong>createScriptProcessor</strong>。</p><p>createAnalyser()方法能创建一个AnalyserNode，可以用来获取音频时间和频率数据，以及实现数据可视化。</p><p>createScriptProcessor()方法创建一个ScriptProcessorNode，用于通过JavaScript处理音频。</p><p>下面几个代码片段是以下demo都会用到的</p><pre class="language-js"><code><span class="token function">initAnalyser</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">//创建分析器</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>analyser <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>audioCtx<span class="token punctuation">.</span><span class="token function">createAnalyser</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//快速傅里叶变换参数</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>analyser<span class="token punctuation">.</span>fftSize <span class="token operator">=</span> <span class="token number">256</span><span class="token punctuation">;</span>
    <span class="token comment">//bufferArray长度</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>bufferLength <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>analyser<span class="token punctuation">.</span>frequencyBinCount<span class="token punctuation">;</span>
    <span class="token comment">//创建bufferArray，用来装音频数据</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>dataArray <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Uint8Array</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>bufferLength<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>

<span class="token function">initScriptProcessor</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">//创建处理器，参数分别是缓存区大小、输入声道数、输出声道数</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>scriptProcessor <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>audioCtx<span class="token punctuation">.</span><span class="token function">createScriptProcessor</span><span class="token punctuation">(</span><span class="token number">2048</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//分析器连接处理器，处理器连接扬声器</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>analyser<span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>scriptProcessor<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>scriptProcessor<span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>audioCtx<span class="token punctuation">.</span>destination<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><p>scriptProcessor可以绑定一个事件audioprocess，当scriptProcessor连接上分析器analyser，且有音频数据时，事件被触发。</p><pre class="language-js"><code><span class="token function">bindDrawEvent</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>scriptProcessor<span class="token punctuation">.</span>onaudioprocess <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>draw<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><p>canvas绘画代码</p><pre class="language-js"><code><span class="token function">draw</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> cWidth <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>canvas<span class="token punctuation">.</span>width<span class="token punctuation">,</span>
        cHeight <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>canvas<span class="token punctuation">.</span>height<span class="token punctuation">,</span>
        barWidth <span class="token operator">=</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token number">.5</span> <span class="token operator">*</span> cWidth <span class="token operator">/</span> <span class="token keyword">this</span><span class="token punctuation">.</span>bufferLength<span class="token punctuation">)</span><span class="token punctuation">,</span>
        barHeight<span class="token punctuation">,</span>
        x <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>canvasCtx<span class="token punctuation">.</span><span class="token function">clearRect</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> cWidth<span class="token punctuation">,</span> cHeight<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//分析器获取音频数据“切片”</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>analyser<span class="token punctuation">.</span><span class="token function">getByteFrequencyData</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>dataArray<span class="token punctuation">)</span><span class="token punctuation">;</span>
	
	<span class="token comment">//把每个音频“切片”画在画布上</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>bufferLength<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        barHeight <span class="token operator">=</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token number">0.4</span> <span class="token operator">*</span> <span class="token keyword">this</span><span class="token punctuation">.</span>dataArray<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>canvasCtx<span class="token punctuation">.</span><span class="token function">fillRect</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> cHeight <span class="token operator">-</span> barHeight<span class="token punctuation">,</span> barWidth<span class="token punctuation">,</span> barHeight<span class="token punctuation">)</span><span class="token punctuation">;</span>
        x <span class="token operator">+=</span> barWidth <span class="token operator">+</span> <span class="token number">3</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre><h3 id="🌰-来自audio元素"><a href="#🌰-来自audio元素" aria-hidden="true" class="header-anchor">#</a> 🌰 来自audio元素</h3><div class="audio-wrapper" data-v-fc6677a4><div class="analyser" data-v-fc6677a4><canvas width="100%" height="100" data-v-fc6677a4></canvas></div><h3 class="title" data-v-fc6677a4>点击播放：</h3><div data-v-fc6677a4><audio controls="controls" src="https://reneroth.org/projects/codepen/dornendiamant.ogg" data-v-fc6677a4></audio></div></div><p>核心代码：</p><pre class="language-js"><code><span class="token keyword">this</span><span class="token punctuation">.</span>audioElement <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">'.audio'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>audioElement<span class="token punctuation">.</span>crossOrigin<span class="token operator">=</span><span class="token string">&quot;anonymous&quot;</span><span class="token punctuation">;</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>audioSource <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>audioCtx<span class="token punctuation">.</span><span class="token function">createMediaElementSource</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>audioElement<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>audioSource<span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>analyser<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>audioSource<span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>gainNode<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">bindDrawEvent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><h3 id="🌰-来自用户设备"><a href="#🌰-来自用户设备" aria-hidden="true" class="header-anchor">#</a> 🌰 来自用户设备</h3><div class="audio-wrapper" data-v-62b8a20a><div class="analyser" data-v-62b8a20a><canvas width="100%" height="100" data-v-62b8a20a></canvas></div><span class="title" data-v-62b8a20a>点击说话 </span><button type="button" class="el-button el-button--default el-button--small" data-v-62b8a20a><!----><i class="el-icon-phone-outline"></i><!----></button></div><p>核心代码：</p><pre class="language-js"><code>navigator<span class="token punctuation">.</span>mediaDevices<span class="token punctuation">.</span><span class="token function">getUserMedia</span><span class="token punctuation">(</span><span class="token punctuation">{</span>audio<span class="token punctuation">:</span> <span class="token boolean">true</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span>stream <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>audioSource <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>audioCtx<span class="token punctuation">.</span><span class="token function">createMediaStreamSource</span><span class="token punctuation">(</span>stream<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>audioSource<span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>analyser<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>audioSource<span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>gainNode<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">bindDrawEvent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span> error <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">'出错，请确保已允许浏览器获取音频权限'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><h3 id="🌰-来自远程音频"><a href="#🌰-来自远程音频" aria-hidden="true" class="header-anchor">#</a> 🌰 来自远程音频</h3><div class="audio-wrapper" data-v-d7e974b0><div class="analyser" data-v-d7e974b0><canvas width="100%" height="100" data-v-d7e974b0></canvas></div><div class="control" data-v-d7e974b0><i class="el-icon-caret-right" data-v-d7e974b0></i></div><div class="song" data-v-d7e974b0><ul data-v-d7e974b0><li data-v-d7e974b0><a href="javascript:;" data-v-d7e974b0>远程音乐</a></li></ul></div></div><p>核心代码：</p><pre class="language-js"><code><span class="token function">initSource</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>audioSource <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>audioCtx<span class="token punctuation">.</span><span class="token function">createBufferSource</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>audioSource<span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>analyser<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>audioSource<span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>gainNode<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>

<span class="token function">onPlay</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">//重新播放需要重新创建buffer</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>audioSource <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>audioCtx<span class="token punctuation">.</span><span class="token function">createBufferSource</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>audioSource<span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>analyser<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>audioSource<span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>audioCtx<span class="token punctuation">.</span>destination<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>audioSource<span class="token punctuation">.</span>buffer <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>buffer<span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>audioSource<span class="token punctuation">.</span>loop <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>audioSource<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>playResume<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">this</span><span class="token punctuation">.</span>playStart <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token keyword">this</span><span class="token punctuation">.</span>playResume <span class="token operator">*</span> <span class="token number">1000</span><span class="token punctuation">;</span>

    <span class="token keyword">this</span><span class="token punctuation">.</span>playing <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>

<span class="token function">onPause</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>playResume <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>playResume <span class="token operator">-=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>playStart<span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>playResume <span class="token operator">/=</span> <span class="token number">1000</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>playResume <span class="token operator">%=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>audioSource<span class="token punctuation">.</span>buffer<span class="token punctuation">.</span>duration<span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>audioSource<span class="token punctuation">.</span><span class="token function">stop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>playing <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>

<span class="token function">play</span> <span class="token punctuation">(</span>url<span class="token punctuation">,</span> index<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>index <span class="token operator">==</span> index<span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>index <span class="token operator">=</span> index<span class="token punctuation">;</span>
    
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">requestSong</span><span class="token punctuation">(</span>url<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>

<span class="token function">requestSong</span> <span class="token punctuation">(</span>url<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> request <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">XMLHttpRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    request<span class="token punctuation">.</span><span class="token function">open</span><span class="token punctuation">(</span><span class="token string">&quot;GET&quot;</span><span class="token punctuation">,</span> url<span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    request<span class="token punctuation">.</span>responseType <span class="token operator">=</span> <span class="token string">&quot;arraybuffer&quot;</span><span class="token punctuation">;</span>

    <span class="token keyword">this</span><span class="token punctuation">.</span>loading <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
    request<span class="token punctuation">.</span><span class="token function-variable function">onload</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>audioCtx<span class="token punctuation">.</span><span class="token function">decodeAudioData</span><span class="token punctuation">(</span>request<span class="token punctuation">.</span>response<span class="token punctuation">,</span> buffer <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>loading <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>playing <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>

            <span class="token keyword">this</span><span class="token punctuation">.</span>buffer <span class="token operator">=</span> buffer<span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">playSound</span><span class="token punctuation">(</span>buffer<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">bindDrawEvent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
    request<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>

<span class="token function">playSound</span> <span class="token punctuation">(</span>buffer<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>audioSource<span class="token punctuation">.</span>buffer <span class="token operator">=</span> buffer<span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>audioSource<span class="token punctuation">.</span>loop <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>audioSource<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>playStart <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><h3 id="🌰-来自本地音频文件"><a href="#🌰-来自本地音频文件" aria-hidden="true" class="header-anchor">#</a> 🌰 来自本地音频文件</h3><div class="audio-wrapper" data-v-d130ebd0><div class="analyser" data-v-d130ebd0><canvas width="100%" height="100" data-v-d130ebd0></canvas></div><div class="file" data-v-d130ebd0><input type="file" accept="audio/*" data-v-d130ebd0><!----></div></div><p>核心代码：</p><pre class="language-js"><code><span class="token function">onProcessFile</span> <span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>file <span class="token operator">=</span> e<span class="token punctuation">.</span>target<span class="token punctuation">.</span>files<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">let</span> stream <span class="token operator">=</span> <span class="token constant">URL</span><span class="token punctuation">.</span><span class="token function">createObjectURL</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>file<span class="token punctuation">)</span><span class="token punctuation">,</span>
        audio <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Audio</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    audio<span class="token punctuation">.</span>src <span class="token operator">=</span> stream<span class="token punctuation">;</span>

    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    audio<span class="token punctuation">.</span>oncanplay <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">initAudioSource</span><span class="token punctuation">(</span>audio<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>showCancel <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>

<span class="token function">onCancel</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>audioCtx<span class="token punctuation">.</span>state <span class="token operator">!=</span> <span class="token string">'closed'</span> <span class="token operator">&amp;&amp;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>audioCtx<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token constant">URL</span><span class="token punctuation">.</span><span class="token function">revokeObjectURL</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>file<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">this</span><span class="token punctuation">.</span>fileInput<span class="token punctuation">.</span>value <span class="token operator">=</span> <span class="token string">''</span><span class="token punctuation">;</span> 
    <span class="token keyword">this</span><span class="token punctuation">.</span>showCancel <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>

<span class="token function">initAudioSource</span> <span class="token punctuation">(</span>audio<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>fileInput <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$refs<span class="token punctuation">[</span><span class="token string">'fileInput'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>audioSource <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>audioCtx<span class="token punctuation">.</span><span class="token function">createMediaElementSource</span><span class="token punctuation">(</span>audio<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>audioSource<span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>analyser<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>audioSource<span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>gainNode<span class="token punctuation">)</span><span class="token punctuation">;</span>

    audio<span class="token punctuation">.</span><span class="token function">play</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">bindDrawEvent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><h2 id="用途"><a href="#用途" aria-hidden="true" class="header-anchor">#</a> 用途</h2><p>可以用来干啥呢？</p><ul><li>游戏音效?</li><li>音乐网站?</li><li>H5宣传页?</li><li>用声音来装饰自己的博客?</li><li>等等</li></ul><p>总之可以出现声音的地方都可以用到。</p><h2 id="参考资料"><a href="#参考资料" aria-hidden="true" class="header-anchor">#</a> 参考资料</h2><ul><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API" target="_blank" rel="noopener noreferrer">Web Audio API</a></li><li><a href="https://www.zhangxinxu.com/wordpress/2017/06/html5-web-audio-api-js-ux-voice/" target="_blank" rel="noopener noreferrer">利用HTML5 Web Audio API给网页JS交互增加声音</a></li><li><a href="https://css-tricks.com/introduction-web-audio-api/" target="_blank" rel="noopener noreferrer">Introduction to Web Audio API</a></li><li><a href="http://www.360doc.com/content/11/0815/08/3416571_140455810.shtml" target="_blank" rel="noopener noreferrer">钢琴各键对应频率</a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API/Visualizations_with_Web_Audio_API" target="_blank" rel="noopener noreferrer">Visualizations with Web Audio API</a></li></ul></div><div class="wx-wrapper" data-v-0de8a4bf><p class="wx-txt" data-v-0de8a4bf>欢迎关注</p><img src="http://pn4meizzc.bkt.clouddn.com/wxqr.png" class="wx-img" data-v-0de8a4bf></div><div class="beg-wrapper" data-v-1f1af99c><a href="javascript:;" class="btn show" data-v-1f1af99c><img src="http://pn4meizzc.bkt.clouddn.com/blogshang_log.png" data-v-1f1af99c></a><p class="shang-txt" data-v-1f1af99c>赏不赏，看您心情</p><img src="http://pn4meizzc.bkt.clouddn.com/blogWechatIMG35.jpeg" class="shangImg" data-v-1f1af99c></div><!----><div class="content page-nav"><p class="inner"><span class="prev">
        ← <a href="/blog/vuepress/js/useful_webapis_mediarecorder.html" class="prev">
          用浏览器轻松录制音频、视频—— MediaRecorder API
        </a></span><span class="next"><a href="/blog/vuepress/js/useful_webapis_fullscreen.html">
          几个有用的Web API——全屏模式
        </a> →
      </span></p></div></div></div></div>
    <script src="/blog/vuepress/assets/js/28.e320efab.js" defer></script><script src="/blog/vuepress/assets/js/94.e993d310.js" defer></script><script src="/blog/vuepress/assets/js/93.c6ca00b3.js" defer></script><script src="/blog/vuepress/assets/js/92.80ab06fd.js" defer></script><script src="/blog/vuepress/assets/js/91.b493a1bb.js" defer></script><script src="/blog/vuepress/assets/js/90.c1e3263e.js" defer></script><script src="/blog/vuepress/assets/js/89.b05297be.js" defer></script><script src="/blog/vuepress/assets/js/app.15770e34.js" defer></script>
  </body>
</html>
